FROM alpine:3.7 as alpine-base
RUN sed -i -e "s/dl-cdn.alpinelinux.org/mirrors.tencentyun.com/" /etc/apk/repositories
RUN apk add git gcc musl-dev libressl-dev

FROM alpine-base as builder
# FROM alpine:3.7 as builder
# RUN apk add git gcc musl-dev

ARG NIM_VERSION
ENV NIM_VERSION ${NIM_VERSION}

WORKDIR /build

COPY curtag .
RUN git clone --depth=1 --branch ${NIM_VERSION} https://gitee.com/hl4/Nim.git

WORKDIR /build/Nim

RUN git clone https://gitee.com/hl4/csources.git

WORKDIR /build/Nim/csources

COPY curtag .

RUN git checkout $(cat curtag) || git checkout $(echo $(git tag) $(cat curtag) | sed 's/ /\n/g' | sed 's/v/0./' | sort -t. -n -k1,1 -k2,2 -k3,3 -k4,4 | sed 's/0./v/' | sed -n "/$(cat curtag)/q;p" | tail -n1)

RUN sh build.sh

WORKDIR /build/Nim

RUN bin/nim c --skipUserCfg --skipParentCfg koch

RUN ./koch boot -d:release

RUN mkdir /build/result

RUN ./koch install /build/result

RUN ./koch nimble

##################################

FROM alpine-base as installer
# FROM alpine:3.7 as installer
# RUN apk add git gcc musl-dev libressl-dev

WORKDIR /installer

COPY --from=builder /build/result/* ./nim/

COPY --from=builder /build/Nim/bin/nimble ./nim/bin

ENV PATH=$PATH:/installer/nim/bin

COPY packages.nimble ./

RUN nimble install --nimbleDir:/installer/nimble -y --depsOnly

##################################

FROM alpine-base as playground
# FROM alpine:3.7 as playground
# RUN apk add gcc g++ musl-dev

RUN apk add lapack # Requirement for arraymancer

RUN apk add pcre # Requirement for regex

WORKDIR /playground

COPY --from=builder /build/result/* ./nim/

COPY --from=installer /installer/nimble/pkgs ./nimble

ENV PATH=$PATH:/playground/nim/bin

RUN mkdir /usercode && chown nobody:nobody /usercode

RUN chown -R nobody:nobody /playground/nimble
